/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-01-26
 * Time: 15:40
 */
public class ListNode {
    public int val;
    public ListNode next;
    public ListNode(int val) {
        this.val = val;

    }
    public ListNode() {
        this.val = val;
    }
    public ListNode reverseList(ListNode head) {
            // 判断链表是否为空
            if (head == null) {
                return null;
            }
            // 定义两个指针cur prve,一个中间指针tmp
            ListNode cur = head;
            ListNode prve = null;
            ListNode tmp = null;
            // 判断条件为cur!=null
            while (cur!= null) {
                tmp = cur.next;
                cur.next = prve;
                prve = cur;
                cur = tmp;
            }
            return prve;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        ListNode head = null;
        //连接节点
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
        ListNode listNode = new ListNode();
        listNode.reverseList(head);
    }
}
